﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SQLHelper;

public partial class admin_News_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["edit"] != null)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
    }

    protected void Update(object sender, EventArgs e)
    {
        int UserID = Master.UserID;
        int id = Convert.ToInt32(Request["edit"]);
        string title = Convert.ToString(TextBox1.Text).Trim();
        string source = Convert.ToString(TextBox2.Text).Trim();
        string contents = Convert.ToString(TextBox3.Text).Trim();
        string sql = "update news set title = '" +
            title + "', source = '" +
            source + "', contents = '" +
            contents + "' where id = " +
            id + " and user_id = " +
            UserID;
        DB.GetDataNoQuery(sql);
        DB.ColseSqlCon();
        Response.Redirect("~/admin/News/Read.aspx?read=" + id);
    }

    void bind()
    {
        int UserID = Master.UserID;
        int id = Convert.ToInt32(Request["edit"]);
        string sql = "select * from news where id = " + id + " and user_id = " + UserID + ";";
        SqlDataReader res = DB.GetDataWithQuery(sql);
        if (res.Read())
        {
            TextBox1.Text = Convert.ToString(res["title"]);
            TextBox2.Text = Convert.ToString(res["source"]);
            TextBox3.Text = Convert.ToString(res["contents"]);
        }
    }
}